package com.gh.controller;

import com.gh.entity.CommonResult;
import com.gh.entity.Payment;
import com.gh.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author Eric
 * @Date 2021/7/27 23:29
 * @Version 1.0
 */
@RestController
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("payment/create")
    public CommonResult create(@RequestBody Payment payment) {
        int add = paymentService.add(payment);
        log.info("插入结果" + add);
        if (add > 0) {
            log.info("插入数据库成功");
            return new CommonResult(200,"插入数据库成功,serverPort="+serverPort,add);
        }
        return new CommonResult(201,"插入失败",null);
    }

    @GetMapping("payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("查询结果" + payment);
        if (payment != null) {
            log.info("查询数据库成功");
            return new CommonResult(200,"查询数据库成功,serverPort="+serverPort,payment);
        }
        return new CommonResult(201,"没有这条记录",null);
    }

    @GetMapping("/payment/lb")
    public String getPaymentLb(){
        return serverPort;
    }
}
